//
//  MoodViewController.m
//  PipeNote
//
//  Created by lanou3g on 15/12/18.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MoodViewController.h"
#import "MoodTableViewCell.h"
#import "moodWebView.h"
#import "JokeDataManager.h"
#import "Model.h"
#import "ShowMoodViewController.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "DCPicScrollView.h"
#import "DCWebImageManager.h"
#import "MJRefresh.h"
#import "DataHandel.h"


@interface MoodViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
    UIImageView *imageView;
}
@property (nonatomic,strong) UITableView *tableView;



@end

@implementation MoodViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"精选推荐";
    //背景图片
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a.jpeg"]];
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:imageView];

    //添加轮播图
    [self lunbo];
    
    //创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height-64)/3+64, self.view.frame.size.width, (self.view.frame.size.height-64)*2/3) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    //网络请求之前
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self judegNet];
               //结束刷新
        [self.tableView.mj_header endRefreshing];

        }];
    //开始刷新
    [self.tableView.mj_header beginRefreshing];


    //上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self judegNet];
        //
        [self.tableView.mj_footer endRefreshing];
    }];
    
    //
    [self.tableView.mj_footer beginRefreshing];

    

   
}

#pragma mark -- 判断是否有网
-(void)judegNet{
    if(![[DataHandel sharedHandel]connectedToNetwork ])
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络连接失败,请查看网络是否连接正常！" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        
    }else{
        
        [self getDataFromNetWork];
    }
    
}




//轮播图
-(void)lunbo
{
    
    //网络加载
    NSArray *UrlStringArray = @[@"http://img.51tietu.net/upload/www.51tietu.net/2015-11/201511290937222113.jpg",
        @"http://img.51tietu.net/upload/www.51tietu.net/2015-11/201511290937268483.jpg",@"http://img.51tietu.net/upload/www.51tietu.net/2015-11/201511290937335710.jpg",@"http://img.51tietu.net/upload/www.51tietu.net/2015-11/201511290937345501.jpg"];
    
    
    NSArray *titleArray = [@"午夜寂寞 谁来陪我,唱一首动人的情歌.你问我说 快不快乐,唱情歌越唱越寂寞.谁明白我 想要什么,一瞬间释放的洒脱.灯光闪烁 不必啰嗦,我就是传说中的那个摇摆哥.我是摇摆哥 音乐会让我快乐,我是摇摆哥 我已忘掉了寂寞.我是摇摆哥 音乐会让我洒脱,我们一起唱这摇摆的歌" componentsSeparatedByString:@"."];
    
    
    //显示顺序和数组顺序一致
    //设置图片url数组,和滚动视图位置
    
    DCPicScrollView  *picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 64, self.view.frame.size.width,(self.view.frame.size.height-64)/3) WithImageUrls:UrlStringArray];
    
    //显示顺序和数组顺序一致
    //设置标题显示文本数组
    
    picView.titleData = titleArray;
    
    //占位图片,你可以在下载图片失败处修改占位图片
    
    picView.placeImage = [UIImage imageNamed:@"place.png"];
    
    //图片被点击事件,当前第几张图片被点击了,和数组顺序一致
    
    [picView setImageViewDidTapAtIndex:^(NSInteger index) {
        printf("第%zd张图片\n",index);
    }];
    
    //default is 2.0f,如果小于0.5不自动播放
    picView.AutoScrollDelay = 3.0f;
    picView.font = [UIFont systemFontOfSize:15];
    picView.textColor = [UIColor blackColor];
    
    [self.view addSubview:picView];
    
    //下载失败重复下载次数,默认不重复,
    [[DCWebImageManager shareManager] setDownloadImageRepeatCount:1];
    
    //图片下载失败会调用该block(如果设置了重复下载次数,则会在重复下载完后,假如还没下载成功,就会调用该block)
    //error错误信息
    //url下载失败的imageurl
    [[DCWebImageManager shareManager] setDownLoadImageError:^(NSError *error, NSString *url) {
        NSLog(@"%@",error);
    }];
    
}

//刷新网络
- (void)getDataFromNetWork{
    
    [[JokeDataManager sharedDataManager] getDataFromNetwithStr:0 Block:^(BOOL isfinished) {
        //
        //取消转圈
        [MBProgressHUD hideHUDForView:self.tableView animated:YES];

        if (isfinished) {
            [self.tableView reloadData];
            
        }
        
  
        
    }];

    
}



#pragma mark - 协议方法 - UITableViewDataSource
//设置每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [JokeDataManager sharedDataManager].jokeDataArray.count;
}
//创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    static NSString *str = @"MoodTableViewCell";
    MoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
         cell = [[[NSBundle mainBundle] loadNibNamed:@"MoodTableViewCell" owner:self options:nil]firstObject];
    }
    
    Model *model = [JokeDataManager sharedDataManager].jokeDataArray[indexPath.row];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.sourceLabel.text = model.source;
    cell.titleLabel.text = model.title;
    
    [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.firstImg] placeholderImage:[UIImage imageNamed:@"4.jpg"]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Model *model = [JokeDataManager sharedDataManager].jokeDataArray[indexPath.row];
    ShowMoodViewController *showMoodVC = [[ShowMoodViewController alloc] init];
    showMoodVC.url = model.url;
    showMoodVC.title = model.title;
    showMoodVC.source = model.source;
    showMoodVC.firstImg = model.firstImg;
    
    
    [self.navigationController pushViewController:showMoodVC animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [UIScreen mainScreen].bounds.size.height/6;
}





@end
